<template>
  <div class="container">
    <h3 class="title">
      <svg-icon icon-class="nested" class="title-icon" />设备管理
    </h3>
    <panel-group></panel-group>

    <el-card>
      <div slot="header">
        <span>设备管理</span>
      </div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="机器编号">
          <el-input v-model="formInline.no" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="所属项目">
          <el-select v-model="formInline.projectId" placeholder="请选择">
            <el-option label="请选择" :value="'0'"></el-option>
            <el-option :label="item.name" :value="item.id" v-for="(item,index) in options"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.deviceStatus" placeholder="请选择">
            <el-option label="请选择" value></el-option>
            <el-option label="正常" value="0"></el-option>
            <el-option label="异常" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="已绑定">
          <el-select v-model="formInline.type" placeholder="请选择">
            <el-option label="请选择" value></el-option>
            <el-option label="未绑定" value="0"></el-option>
            <el-option label="已绑定" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
      <div style="margin-bottom: 10px;">
        <el-button type="primary" size="small" @click="add">新增设备</el-button>
      </div>
      <div style="border-top:1px solid #e6ebf5;margin-bottom:10px;">
        <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%;margin-bottom:10px;min-height:calc(100vh - 650px)"
        >
          <el-table-column prop="id" label="序号"></el-table-column>
          <el-table-column prop="no" label="机器编号"></el-table-column>
          <el-table-column prop="projectName" label="所属项目"></el-table-column>
          <el-table-column label="状态">
            <template
              slot-scope="scope"
            >{{scope.row.deviceStatus==0?'正常':"异常"}} {{scope.row.deviceStatus==1?scope.row.deviceStatusReason:""}}</template>
          </el-table-column>
          <el-table-column label="已绑定">
            <template
              slot-scope="scope"
            >{{scope.row.projectName==''||scope.row.projectName==null?'未绑定':"已绑定"}}</template>
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <template slot-scope="scope">
              <span style="color:#409EFF;cursor:pointer;" @click="handleDetail(scope.row.id)">详情</span>
              <span style="color:#409EFF;cursor:pointer;" @click="edit(scope.row.id)">编辑</span>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-if="totalPage>1"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[100, 200, 300, 400]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalPage"
          style="float: right;"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getDeviceList, getNum } from "@/api/device";
import { getProjectList } from "@/api/project";
import PanelGroup from "./components/PanelGroup";
export default {
  name: "manage",
  components: {
    PanelGroup
  },
  data() {
    return {
      formInline: {
        no: "",
        projectId: "",
        type: "",
        deviceStatus: ""
      },
      pageSize: 10,
      options: [],
      currentPage: 1,
      totalPage: 0,
      tableData: []
    };
  },
  mounted() {
    this.getP();
    this.getList();
  },
  methods: {
    handleDetail(id) {
      this.$router.push({ path: "/manage/detail", query: { id: id } });
    },
    edit(id) {
      this.$router.push({ path: "/manage/edit", query: { id: id } });
    },
    add() {
      this.$router.push({
        path: "/manage/edit"
      });
    },
    getP() {
      getProjectList("", 1, 500, 1).then(res => {
        console.log(res);
        this.options = res.data;
      });
    },
    handleSearch() {
      this.getList();
    },
    getList() {
      getDeviceList(
        this.formInline.projectId == 0 ? "" : this.formInline.projectId,
        this.formInline.no,
        this.formInline.deviceStatus,
        this.formInline.type,
        this.currentPage,
        this.pageSize,
        1
      ).then(res => {
        console.log(res);
        this.tableData = res.data;
        this.totalPage = res.recordsTotal;
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getList();
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
  background-color: rgb(240, 242, 245);
  position: relative;
  .title {
    font-size: 14px;
    font-weight: 400;
    .title-icon {
      font-size: 14px;
      display: inline-block;
      margin-right: 5px;
    }
  }
  /deep/ .el-card__header {
    border: 0 none;
  }
}
.title-small {
  font-size: 14px;
}

.money {
  color: #409eff;
}
</style>
